/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.util;

import java.nio.charset.Charset;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author haolw
 * @time 2016/09/29
 */
public class EncryptUtil {
    private static Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private static final String MD5_SALT = "$SALT$";

    private static final String KEY = "3ffa79e997ed11e9a2620235d2b38928";

    /**
     * byte数组编码为base64字符串
     *
     * @param bytes
     * @return
     */
    public static String encodeBase64(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    /**
     * 字符串文本编码为base64字符串
     *
     * @param text
     * @return
     */
    public static String encodeBase64(String text) {
        return encodeBase64(stringToBytes(text));
    }

    /**
     * byte数组编码为安全的base64字符串
     *
     * @param bytes
     * @return
     */
    public static String encodeBase64Safe(byte[] bytes) {
        return Base64.encodeBase64URLSafeString(bytes);
    }

    /**
     * 字符串文本编码为安全的base64字符串
     *
     * @param text
     * @return
     */
    public static String encodeBase64Safe(String text) {
        return encodeBase64Safe(stringToBytes(text));
    }

    /**
     * base64二进制数组解码
     *
     * @param bytes
     * @return
     */
    public static String decodeBase64(byte[] bytes) {
        return bytesToString(Base64.decodeBase64(bytes));
    }

    /**
     * base64文本解码
     *
     * @param text
     * @return
     */
    public static String decodeBase64(String text) {
        return bytesToString(Base64.decodeBase64(text));
    }

    /**
     * 文本转换为位二进制数组
     *
     * @param text
     * @return
     */
    public static byte[] stringToBytes(String text) {
        return text.getBytes(DEFAULT_CHARSET);
    }

    /**
     * 二进制数组转换为文本字符串
     *
     * @param bytes
     * @return
     */
    public static String bytesToString(byte[] bytes) {
        return new String(bytes, DEFAULT_CHARSET);
    }

    /**
     * md5加密 32
     *
     * @param txt
     * @return
     */
    public static String md5(String txt) {
        return DigestUtils.md5Hex(txt);
    }

    public static String hmac256(String txt) {
        byte[] hmac = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, KEY).hmac(txt);
        return Base64.encodeBase64String(hmac);
    }

    /**
     * md5加密 16
     *
     * @param txt
     * @return
     */
    public static String md5_16(String txt) {
        return md5(txt).substring(8, 24);
    }

    /**
     * @param txt
     * @return
     */
    public static String md5AndSalt(String txt) {
        if (StringUtils.isBlank(txt)) {
            return null;
        }
        txt += MD5_SALT;
        return md5(txt);
    }

    /**
     * @param txt
     * @return
     */
    public static String md5AndSalt_16(String txt) {
        return md5AndSalt(txt).substring(8, 24);
    }

    public static String aesEncrypt(String content) {
        return AESEncryption.getInstrance().encrypt(content);
    }

    public static String aesDecrypt(String content) {
        return AESEncryption.getInstrance().decrypt(content);
    }

}
